package com.game;

import com.game.beans.*;
import com.game.utils.GameState;
import com.game.utils.GameUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public class GameWindow extends JFrame {
    public static GameState state = GameState.START;
    public static int WIDTH = 480;
    public static int HEIGHT = 852;
    private int threadMil = 25;
    public static int count = 0;
    public static int enemyDestoried = 0;
    public BackgroundBean BGBean = new BackgroundBean(GameUtils.bgImg,0,0,0);
    public HeroBean HBean = new HeroBean(GameUtils.heroImg,WIDTH / 2,HEIGHT - 100,100,125,0,this);
    public Image offScreamImage = null;
    public Random random = new Random();

    public static int shellPrid = 25;
    public static int shellChange = 20;

    public static int enemySpeed = 3;
    public static int enemySpeedChange = 25;

    public static int enemyPrid = 15;
    public static int enemyPridChange = 20;


    public static void main(String[] args) {
        GameWindow gameWindow = new GameWindow();
        gameWindow.launch();
    }

    public void launch(){

        this.setVisible(true);

        this.setSize(WIDTH,HEIGHT);

        this.setLocationRelativeTo(null);

        this.setTitle("Plane");

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        this.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton() == 1 && state == GameState.START || state == GameState.GAMEOVER ){
                    state = GameState.RUNNING;
                    count = 0;
                    enemyDestoried = 0;
                    GameUtils.gameBeans.clear();
                    GameUtils.shellBeans.clear();
                    GameUtils.enemyBeans.clear();
                    GameUtils.removeBeans.clear();
                    repaint();
                }

            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        while(true){
            repaint();
            try {
                Thread.sleep(threadMil);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }


    private void createBean(){
        if (state == GameState.RUNNING){
            shellPrid = Math.max(5,20 - (int)(enemyDestoried / shellChange));
            enemyPrid = Math.max(8,15 - (int)(enemyDestoried / enemyPridChange));
            enemySpeed = Math.min(10,3 + (int)(enemyDestoried / enemySpeedChange));
            if (count % shellPrid == 0){
                GameUtils.shellBeans.add(new ShellBean(GameUtils.bulletImg,HBean.getX() + HBean.getX() / 4,HBean.getY() - HBean.getHeight() / 4  ,8,14,-3,this));
                GameUtils.gameBeans.add(GameUtils.shellBeans.get(GameUtils.shellBeans.size() - 1));
            }

            if (count % enemyPrid == 0){
                GameUtils.enemyBeans.add(new EnemyBean(GameUtils.airplaneImg,(int)((GameWindow.WIDTH - 60) * random.nextDouble()) + 25,40  ,49,36,enemySpeed,this));
                GameUtils.gameBeans.add(GameUtils.enemyBeans.get(GameUtils.enemyBeans.size() - 1));
            }
            count++;
        }
    }

    private void removeBean(){
        if (state == GameState.RUNNING){
            for (GameBean gameBean:GameUtils.removeBeans
                 ) {
                GameUtils.gameBeans.remove(gameBean);
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        if (offScreamImage == null){
            offScreamImage = createImage(WIDTH,HEIGHT);
        }
        Graphics gImage = offScreamImage.getGraphics();
        gImage.fillRect(0,0,WIDTH,HEIGHT);
        if(state == GameState.START){
            gImage.drawImage(GameUtils.bgImg,0,0,null);
            gImage.drawImage(GameUtils.startImg,50,100,null);

            GameUtils.drawWords(gImage,"点击开始游戏 ",Color.BLUE,80,500,50);

        }
        else if (state == GameState.RUNNING){

            createBean();

            BGBean.paintSelf(gImage);
            HBean.paintSelf(gImage);
            for (GameBean gameBean:GameUtils.gameBeans
                 ) {
                gameBean.paintSelf(gImage);
            }
            removeBean();
        }
        else if (state == GameState.GAMEOVER){
            gImage.drawImage(GameUtils.bgImg,0,0,null);
            gImage.drawImage(GameUtils.gameOverImg,40,100,null);

            GameUtils.drawWords(gImage,"点击重新开始游戏 ",Color.BLUE,80,500,40);

        }
        GameUtils.drawWords(gImage,"得分 " + Integer.toString(enemyDestoried),Color.BLUE,20,80,30);
        g.drawImage(offScreamImage,0,0,null);

    }


}
